/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO.InformationFilter;

import DAO.DAOAccess;
import Info.PeriodInfo;
import com.mysql.jdbc.Connection;
import java.security.Timestamp;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class PrdDAO
{
    private static PrdDAO m_instance = new PrdDAO();
    private PrdDAO(){}

    public static PrdDAO GetInstance()
    {
        return m_instance;
    }

    public static void DoEmpty()
    {
         try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "DELETE FROM PERIOD";
            PreparedStatement stat = conn.prepareCall(sql);
            stat.execute();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public List<PeriodInfo> GetAllPeriod()
    {
        String query = "SELECT * FROM PERIOD";
        return ExcuteQuery(query);
    }
    public List<PeriodInfo> ExcuteQuery(String query)
    {
        List<PeriodInfo> result = new ArrayList<PeriodInfo>();

        try
        {
            ResultSet rs =  DAOAccess.ExecuteQuery(query);

            int idxID = rs.findColumn("id");
            int idxBegin = rs.findColumn("begin");
            int idxEnd = rs.findColumn("end");

            rs.beforeFirst();

            while(rs.next())
            {
                PeriodInfo periodInfo = new PeriodInfo();
                periodInfo.SetID(rs.getInt(idxID));
                periodInfo.SetBegin(rs.getTimestamp(idxBegin));
                periodInfo.SetEnd(rs.getTimestamp(idxEnd));


                result.add(periodInfo);
            }
            rs.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return result;
    }

    public static int Insert(Date dBegin, Date dEnd)
    {
        int id = -1;
        try
        {
            java.sql.Timestamp begin = new java.sql.Timestamp(dBegin.getTime());
            java.sql.Timestamp end = new java.sql.Timestamp(dEnd.getTime());

            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "INSERT INTO PERIOD(BEGIN, END) VALUES(?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            statement.setTimestamp(1, begin);
            statement.setTimestamp(2, end);
            statement.executeUpdate();
            ResultSet result = statement.getGeneratedKeys();
            result.first();
            id = result.getInt(1);
            statement.close();

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return id;
    }
  
    public static void main(String[] argvs)
    {
        PrdDAO dao = new PrdDAO();
        List<PeriodInfo> lst = dao.GetAllPeriod();
        System.out.println(lst.size());
    }
}
